package com.sctu.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.sctu.EMSApp.R;
import com.sctu.adapter.PlayHotelListViewAdapter;
import com.sctu.adapter.ViewPagerAdapter;
import com.sctu.bean.PlayHotelHeadBean;
import com.sctu.bean.PlayHotelListBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhu on 2015/11/7.
 */
public class PlayHotelFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private View view;
    private ImageView imageView;
    private ViewPager viewPager;
    private ListView listView;
    private List<View> viewList = new ArrayList<View>();
    private List<ImageView> imageViewList = new ArrayList<ImageView>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_play_hotel, container, false);
        initView();
        return view;
    }

    private void initView() {
        viewPager = (ViewPager) view.findViewById(R.id.fragment_play_hotel_viewPager);
        listView = (ListView) view.findViewById(R.id.fragment_play_hotel_listView);
        viewPager.setOnPageChangeListener(this);
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_HOTEL_HEAD,
                listener, errorlistener);
        GetVolleyHttp.getInstance(getActivity()).getStringGETJsonData(UrlConfig.URL_HOTEL_LIST,
                listenerListView, errorlistener);

    }

    // 当网络请求返回成功（200的状态）
    Response.Listener<String> listener = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            List<PlayHotelHeadBean.Results> playHotelHead = JSON.parseObject(json, PlayHotelHeadBean.class).getResults();
            //获取点击图片父控件
            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.fragment_play_hotel_lay);
            for (int i = 0; i < playHotelHead.size(); i++) {
                LinearLayout linearLayout1 = new LinearLayout(getActivity());
                ViewGroup.LayoutParams ltp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                LinearLayout.LayoutParams margin = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                final ImageView itemImageView = new ImageView(getActivity());
                itemImageView.setScaleType(ImageView.ScaleType.FIT_XY);
                GetVolleyHttp.getInstance(getActivity()).setImageLoader(itemImageView,
                        R.drawable.error_image, R.drawable.error_image, UrlConfig.URL + playHotelHead.get(i).getPath());
                linearLayout1.addView(itemImageView, ltp);
                viewList.add(linearLayout1);
                //设置小圆点
                ImageView imageView = new ImageView(getActivity());
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                margin.setMargins(10, 0, 0, 0);
                imageViewList.add(imageView);
                //如果图片只有一张 则不显示
                if (playHotelHead.size() > 1) {
                    if (i == 0) {
                        // 默认选中第一张图片
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_click);
                    } else {
                        //其他图片都设置未选中状态
                        imageView.setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
                    }
                }
                //将image添加到布局中
                linearLayout.addView(imageView, margin);
            }
            viewPager.setAdapter(new ViewPagerAdapter(viewList));
        }
    };

    Response.Listener<String> listenerListView = new Response.Listener<String>() {
        @Override
        public void onResponse(String json) {
            List<PlayHotelListBean.Results> playHotelList = JSON.parseObject(json, PlayHotelListBean.class).getResults();
            listView.setAdapter(new PlayHotelListViewAdapter(getActivity(),playHotelList));
        }
    };


    // 当网络请求返回状态不成功（也就不是200）
    Response.ErrorListener errorlistener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError arg0) {
            Toast.makeText(getActivity(), "网络出了些小问题", Toast.LENGTH_LONG)
                    .show();
        }
    };

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int arg0) {
//遍历数组让当前选中图片下的小圆点设置颜色
        for (int i = 0; i < imageViewList.size(); i++) {
            imageViewList.get(i).setImageResource(R.drawable.scenic_tour_round_after_click_click);
            if (arg0 != i) {
                imageViewList.get(i)
                        .setImageResource(R.drawable.scenic_tour_round_after_click_unclick);
            }

        }
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
